/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic;

import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Freshman
 */
public class DataTable {

    private String tabName = "";    //Tabellenname
    private Object[] colHeader;     //Spaltennamen
    private Object[][] tabData;     //Tabellendaten

    public DataTable(String tn, Object[] ch, Object[][] td) {
        this.tabName = tn;
        this.colHeader = ch;
        this.tabData = td;
    }

    public JTable getAsTable() {
        DefaultTableModel defTableModel = new DefaultTableModel();
        defTableModel.setDataVector(tabData, colHeader);
        JTable table = new JTable(defTableModel) {
        //Editieren der Zellen deaktivieren-------------------------------------
            @Override
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };
        //----------------------------------------------------------------------
        return table;
    }
    
    public String getDataAtPosition(int r, int c) {
        String s = (String) tabData[r][c].toString();
        return s;
    }
    
    public void setDataAtPosition(String s, int r, int c) {
        tabData[r][c] = s;
    }
    
    public ArrayList<String> toCSV() {
        ArrayList<String> as = new ArrayList<String>();
        String s = ""; //String der die Variable bekommt
        String replaced = ""; //Nachdem ; gegen , ausgetauscht wurden, wird s an replaced übergeben
        int sLength = -1; //Länge des replaced String um das letzte ";" zu entfernen
        String korrigiert = ""; //Der von beiden ";" Fehlerquellen beseitigte String
        
        //Spaltennamen
        for (int i = 0; i < colHeader.length; i++) {
            s = (String) colHeader[i].toString();
            replaced += s.replace(';', ',');
            replaced += ";";
        }
        //Das Semikolon am Ende der Zeile entfernen
        sLength = replaced.length();
        korrigiert = replaced.substring(0, (sLength-1));
        as.add(korrigiert);
        
        //Tabellendaten
        replaced = "";
        String lineSep = System.getProperty("line.separator");
        for (int i = 0; i < tabData.length; i++) {
            for (int j = 0; j < tabData[i].length; j++) {
                s = (String) tabData[i][j].toString();
                String x = s.replace(lineSep, " ");
                replaced += x.replace(';', ',');
                replaced += ";";
            }
            sLength = replaced.length();
            korrigiert = replaced.substring(0, (sLength-1));
            as.add(korrigiert);
            replaced = "";
        }
        return as;
    }

    //GETTER UND SETTER---------------------------------------------------------
    public Object[] getColHeader() {
        return colHeader;
    }

    public void setColHeader(Object[] colHeader) {
        this.colHeader = colHeader;
    }

    public String getTabName() {
        return tabName;
    }

    public void setTabName(String tabName) {
        this.tabName = tabName;
    }

    public Object[][] getTabData() {
        return tabData;
    }

    public void setTabData(Object[][] tabData) {
        this.tabData = tabData;
    }
    //--------------------------------------------------------------------------
}
